package com.aidex.biz.utils;

import com.aidex.common.utils.AESUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.ByteArrayOutputStream;
import java.util.Base64;

/**
 * 二维码加密解密
 *
 * @anthor sean-xiong
 * @date 2022-10-08
 */
public class QrCodeUtils {

    /**
     * 二维码信息加密
     * * @param userId
     *
     * @return sean-xiong
     * 2022-10-08
     */
    public static String encrypt(String userId) throws Exception {
        String info = userId;
        return AESUtils.encryptByAES(info);
    }

    /**
     * 解密二维码信息
     *
     * @param aesData
     * @return
     * @throws Exception
     */
    public static String decrypt(String aesData) throws Exception {
        return AESUtils.decryptByAES(aesData);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(decrypt("CFAE5C4F785C2B48783F9D44AFB205968904D136AE50F954D03DF2ABEE36BAFEC57EA380D814014EB2459AB231805AA36A7741F4D8A3F09D798B2BFDF0E5DEDDCF9AA2D48E8D3E53E55A521DB757D5AFABEF0BE21FFBAA66E9D1215EA59F6C3D"));
    }

    public static String getImageBase64(String content) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 600, 600);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
            Base64.Encoder encoder = Base64.getEncoder();
            return encoder.encodeToString(outputStream.toByteArray());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;
    }


}
